package ca.uwo.android.campusmap;

import java.io.CharArrayWriter;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class SaxBuildingHandler extends DefaultHandler {
	// Data
	private BuildingList bl = new BuildingList();
	private BuildingData currentBuilding = null;
	private CharArrayWriter content = new CharArrayWriter();
	
	// Accessor
	public BuildingList getBuildings() {
		return bl;
	}
	
	// Handler
	public void startElement(String namespaceURI, String tag, String qName, Attributes atts) {
		content.reset();
		
		if(tag.equalsIgnoreCase(BuildingList.BUILDING_DATA)) {
			currentBuilding = new BuildingData();
			bl.addBuilding(currentBuilding);
		}
	}
	
	public void endElement(String namespaceURI, String tag, String qName ) {
		// set values
		if(tag.equalsIgnoreCase(BuildingData.BUILDING_NAME)) {
			currentBuilding.setBuilding(content.toString().trim());
			return;
		}
		
		if(tag.equalsIgnoreCase(BuildingData.LATITUDE)) {
			currentBuilding.setLatitude(content.toString().trim());
			return;
		}
		if(tag.equalsIgnoreCase(BuildingData.LONGITUDE)) {
			currentBuilding.setLongitude(content.toString().trim());
			return;
		}
		
		if(tag.equalsIgnoreCase(BuildingData.ADDRESS)) {
			currentBuilding.setAddress(content.toString().trim());
			return;
		}
	}
	
	public void characters(char[] c, int start, int length) {
		content.write(c, start, length);
	}
	
	public BuildingData getBuildingData() {
		return currentBuilding;
	}
	
	public BuildingList getBuildingList() {
		return bl;
	}
}
